<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>处理HTTP请求 | anyang的博客</title>
    <meta name="description" content="千里之行,始于足下">
    <meta name="generator" content="VuePress 1.3.1">
    <link rel="icon" href="/img/favicon.ico">
  <script charset="utf-8" src="/js/main.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.slim.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.2/jquery.fancybox.min.js"></script>
  <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.2/jquery.fancybox.min.css">
  <meta name="keywords" content="福小林,奔奔,ourLang,CentOS,JAVA,vue组件,lsdCloud">
  <script async="async" src="https://www.googletagmanager.com/gtag/js?id=UA-146891701-1"></script>
  <script>window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
    
      gtag('config', 'UA-146891701-1');</script>
  <script>var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?56eae8eec590ccaef1d5ff99d766f315";
        var s = document.getElementsByTagName("script")[0]; 
        s.parentNode.insertBefore(hm, s);
      })();</script>
    
    <link rel="preload" href="/assets/css/0.styles.9fff9873.css" as="style"><link rel="preload" href="/assets/js/app.1bc80adb.js" as="script"><link rel="preload" href="/assets/js/2.7c0608ab.js" as="script"><link rel="preload" href="/assets/js/100.05bb269d.js" as="script"><link rel="prefetch" href="/assets/js/10.81caa2ab.js"><link rel="prefetch" href="/assets/js/101.3e18b4d6.js"><link rel="prefetch" href="/assets/js/102.c7d7ed9c.js"><link rel="prefetch" href="/assets/js/103.4c771ce7.js"><link rel="prefetch" href="/assets/js/104.a2acd367.js"><link rel="prefetch" href="/assets/js/105.63399350.js"><link rel="prefetch" href="/assets/js/106.8a4744cc.js"><link rel="prefetch" href="/assets/js/107.7739d796.js"><link rel="prefetch" href="/assets/js/108.8008ae33.js"><link rel="prefetch" href="/assets/js/109.01c63898.js"><link rel="prefetch" href="/assets/js/11.7b837560.js"><link rel="prefetch" href="/assets/js/110.ad01aca9.js"><link rel="prefetch" href="/assets/js/111.3d74db70.js"><link rel="prefetch" href="/assets/js/112.67f55c34.js"><link rel="prefetch" href="/assets/js/113.016cd3c0.js"><link rel="prefetch" href="/assets/js/114.860e4b2b.js"><link rel="prefetch" href="/assets/js/115.b2f26258.js"><link rel="prefetch" href="/assets/js/116.8bea67b4.js"><link rel="prefetch" href="/assets/js/117.20682843.js"><link rel="prefetch" href="/assets/js/118.86a22e16.js"><link rel="prefetch" href="/assets/js/119.ad518bea.js"><link rel="prefetch" href="/assets/js/12.8fd78714.js"><link rel="prefetch" href="/assets/js/120.f97dd86f.js"><link rel="prefetch" href="/assets/js/121.c5601b4a.js"><link rel="prefetch" href="/assets/js/122.2889645a.js"><link rel="prefetch" href="/assets/js/13.ebf26820.js"><link rel="prefetch" href="/assets/js/14.b92dca09.js"><link rel="prefetch" href="/assets/js/15.b3dcef45.js"><link rel="prefetch" href="/assets/js/16.50378990.js"><link rel="prefetch" href="/assets/js/17.056bba2b.js"><link rel="prefetch" href="/assets/js/18.9047bb38.js"><link rel="prefetch" href="/assets/js/19.ab53b0fe.js"><link rel="prefetch" href="/assets/js/20.fe0b8af0.js"><link rel="prefetch" href="/assets/js/21.20a702f3.js"><link rel="prefetch" href="/assets/js/22.0ee08d29.js"><link rel="prefetch" href="/assets/js/23.02039a9a.js"><link rel="prefetch" href="/assets/js/24.363d3d74.js"><link rel="prefetch" href="/assets/js/25.dd6b101e.js"><link rel="prefetch" href="/assets/js/26.20493714.js"><link rel="prefetch" href="/assets/js/27.708723e5.js"><link rel="prefetch" href="/assets/js/28.4561834a.js"><link rel="prefetch" href="/assets/js/29.15cfcf5f.js"><link rel="prefetch" href="/assets/js/3.8d39d40c.js"><link rel="prefetch" href="/assets/js/30.04ab69de.js"><link rel="prefetch" href="/assets/js/31.b11c4302.js"><link rel="prefetch" href="/assets/js/32.81ae74ee.js"><link rel="prefetch" href="/assets/js/33.ddf09cc0.js"><link rel="prefetch" href="/assets/js/34.bc3549f7.js"><link rel="prefetch" href="/assets/js/35.4a0e0ebd.js"><link rel="prefetch" href="/assets/js/36.961a1c50.js"><link rel="prefetch" href="/assets/js/37.d8658de5.js"><link rel="prefetch" href="/assets/js/38.f76063aa.js"><link rel="prefetch" href="/assets/js/39.73edd08f.js"><link rel="prefetch" href="/assets/js/4.916af9bf.js"><link rel="prefetch" href="/assets/js/40.72314bff.js"><link rel="prefetch" href="/assets/js/41.c5eb1147.js"><link rel="prefetch" href="/assets/js/42.07aeafbb.js"><link rel="prefetch" href="/assets/js/43.c4a652ba.js"><link rel="prefetch" href="/assets/js/44.91fbe22b.js"><link rel="prefetch" href="/assets/js/45.6eecf0c4.js"><link rel="prefetch" href="/assets/js/46.81b7f108.js"><link rel="prefetch" href="/assets/js/47.b4468ab3.js"><link rel="prefetch" href="/assets/js/48.df205f23.js"><link rel="prefetch" href="/assets/js/49.7df59fa3.js"><link rel="prefetch" href="/assets/js/5.4fcd540a.js"><link rel="prefetch" href="/assets/js/50.0dc2fc5b.js"><link rel="prefetch" href="/assets/js/51.1ffabe54.js"><link rel="prefetch" href="/assets/js/52.2269d284.js"><link rel="prefetch" href="/assets/js/53.81b23843.js"><link rel="prefetch" href="/assets/js/54.b3249def.js"><link rel="prefetch" href="/assets/js/55.50dcf2af.js"><link rel="prefetch" href="/assets/js/56.c399c272.js"><link rel="prefetch" href="/assets/js/57.024f1dbb.js"><link rel="prefetch" href="/assets/js/58.680c6cf4.js"><link rel="prefetch" href="/assets/js/59.2291e9d7.js"><link rel="prefetch" href="/assets/js/6.7610d1d4.js"><link rel="prefetch" href="/assets/js/60.e59a26c1.js"><link rel="prefetch" href="/assets/js/61.ff5c64e4.js"><link rel="prefetch" href="/assets/js/62.7d97062c.js"><link rel="prefetch" href="/assets/js/63.6456dc7e.js"><link rel="prefetch" href="/assets/js/64.0b055680.js"><link rel="prefetch" href="/assets/js/65.9f25601c.js"><link rel="prefetch" href="/assets/js/66.cd973084.js"><link rel="prefetch" href="/assets/js/67.9b8f9716.js"><link rel="prefetch" href="/assets/js/68.d6c1869a.js"><link rel="prefetch" href="/assets/js/69.0a3688dd.js"><link rel="prefetch" href="/assets/js/7.9ebbd956.js"><link rel="prefetch" href="/assets/js/70.22f48891.js"><link rel="prefetch" href="/assets/js/71.9eb4fda9.js"><link rel="prefetch" href="/assets/js/72.ca4fa80c.js"><link rel="prefetch" href="/assets/js/73.17d1ab37.js"><link rel="prefetch" href="/assets/js/74.139691db.js"><link rel="prefetch" href="/assets/js/75.08d0560f.js"><link rel="prefetch" href="/assets/js/76.61078c10.js"><link rel="prefetch" href="/assets/js/77.4482adcc.js"><link rel="prefetch" href="/assets/js/78.166f2c37.js"><link rel="prefetch" href="/assets/js/79.2c7d6b79.js"><link rel="prefetch" href="/assets/js/8.6f8916c2.js"><link rel="prefetch" href="/assets/js/80.80445c3f.js"><link rel="prefetch" href="/assets/js/81.14e3bae8.js"><link rel="prefetch" href="/assets/js/82.7dac687f.js"><link rel="prefetch" href="/assets/js/83.1dd2e78e.js"><link rel="prefetch" href="/assets/js/84.23b759de.js"><link rel="prefetch" href="/assets/js/85.7574f69f.js"><link rel="prefetch" href="/assets/js/86.102dac61.js"><link rel="prefetch" href="/assets/js/87.8f5d639e.js"><link rel="prefetch" href="/assets/js/88.e564cbbe.js"><link rel="prefetch" href="/assets/js/89.cd66eb78.js"><link rel="prefetch" href="/assets/js/9.baa162db.js"><link rel="prefetch" href="/assets/js/90.685c6878.js"><link rel="prefetch" href="/assets/js/91.b8d1b7ea.js"><link rel="prefetch" href="/assets/js/92.1faa374d.js"><link rel="prefetch" href="/assets/js/93.cf5ff33c.js"><link rel="prefetch" href="/assets/js/94.8a02aefb.js"><link rel="prefetch" href="/assets/js/95.1b379a5b.js"><link rel="prefetch" href="/assets/js/96.9fd67185.js"><link rel="prefetch" href="/assets/js/97.c5a6a5a1.js"><link rel="prefetch" href="/assets/js/98.9a231411.js"><link rel="prefetch" href="/assets/js/99.5b344cab.js">
    <link rel="stylesheet" href="/assets/css/0.styles.9fff9873.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">anyang的博客</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/study-guide/introduction.html" class="nav-link">
  学习指南
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术分类" class="dropdown-title"><span class="title">技术分类</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/java/introduction.html" class="nav-link">
  Java
</a></li><li class="dropdown-item"><!----> <a href="/go/introduction.html" class="nav-link">
  Go
</a></li><li class="dropdown-item"><!----> <a href="/sql/introduction.html" class="nav-link">
  SQL
</a></li><li class="dropdown-item"><!----> <a href="/fhir/introduction.html" class="nav-link">
  FHIR
</a></li></ul></div></div><div class="nav-item"><a href="/blog/introduction.html" class="nav-link">
  个人博客
</a></div><div class="nav-item"><a href="/tool/developmentBox.html" class="nav-link">
  开发百宝箱
</a></div><div class="nav-item"><a href="/project/introduction.html" class="nav-link">
  项目分享
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="了解更多" class="dropdown-title"><span class="title">了解更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="http://wpa.qq.com/msgrd?v=3&amp;uin=1300378587&amp;site=qq&amp;menu=yes" target="_blank" rel="noopener noreferrer" class="nav-link external">
  其他合作
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="http://www.lsdcloud.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  友情网站
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://github.com/ourlang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/anlexanyang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  码云
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/qq_37493556" target="_blank" rel="noopener noreferrer" class="nav-link external">
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div><div class="nav-item"><a href="/linkExchanges/introduction.html" class="nav-link">
  友情链接
</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/study-guide/introduction.html" class="nav-link">
  学习指南
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术分类" class="dropdown-title"><span class="title">技术分类</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/java/introduction.html" class="nav-link">
  Java
</a></li><li class="dropdown-item"><!----> <a href="/go/introduction.html" class="nav-link">
  Go
</a></li><li class="dropdown-item"><!----> <a href="/sql/introduction.html" class="nav-link">
  SQL
</a></li><li class="dropdown-item"><!----> <a href="/fhir/introduction.html" class="nav-link">
  FHIR
</a></li></ul></div></div><div class="nav-item"><a href="/blog/introduction.html" class="nav-link">
  个人博客
</a></div><div class="nav-item"><a href="/tool/developmentBox.html" class="nav-link">
  开发百宝箱
</a></div><div class="nav-item"><a href="/project/introduction.html" class="nav-link">
  项目分享
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="了解更多" class="dropdown-title"><span class="title">了解更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="http://wpa.qq.com/msgrd?v=3&amp;uin=1300378587&amp;site=qq&amp;menu=yes" target="_blank" rel="noopener noreferrer" class="nav-link external">
  其他合作
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="http://www.lsdcloud.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  友情网站
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://github.com/ourlang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/anlexanyang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  码云
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/qq_37493556" target="_blank" rel="noopener noreferrer" class="nav-link external">
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div><div class="nav-item"><a href="/linkExchanges/introduction.html" class="nav-link">
  友情链接
</a></div> <!----></nav>  <ul class="sidebar-links"><li><a href="/go/introduction.html" class="sidebar-link">Go语言</a></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Go教程</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>Go Web框架</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/go/web/iris-go/start.html" class="sidebar-link">Iris概览</a></li><li><a href="/go/web/iris-go/config.html" class="sidebar-link">配置信息</a></li><li><a href="/go/web/iris-go/handle-http.html" class="active sidebar-link">处理HTTP请求</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/web/iris-go/handle-http.html#_1-数据请求方式的分类" class="sidebar-link">1 数据请求方式的分类</a></li><li class="sidebar-sub-header"><a href="/go/web/iris-go/handle-http.html#_2-iris请求处理方式" class="sidebar-link">2 IRIS请求处理方式</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/web/iris-go/handle-http.html#_2-1-get请求" class="sidebar-link">2.1 GET请求</a></li><li class="sidebar-sub-header"><a href="/go/web/iris-go/handle-http.html#_2-2-post请求" class="sidebar-link">2.2 POST请求</a></li><li class="sidebar-sub-header"><a href="/go/web/iris-go/handle-http.html#_2-3-put-delete等请求" class="sidebar-link">2.3 PUT &amp; DELETE等请求</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/web/iris-go/handle-http.html#_3-请求处理的数据格式返回" class="sidebar-link">3 请求处理的数据格式返回</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/web/iris-go/handle-http.html#_3-1-返回string类型" class="sidebar-link">3.1 返回string类型</a></li><li class="sidebar-sub-header"><a href="/go/web/iris-go/handle-http.html#_3-2-返回json格式" class="sidebar-link">3.2 返回json格式</a></li><li class="sidebar-sub-header"><a href="/go/web/iris-go/handle-http.html#_3-3-返回xml格式" class="sidebar-link">3.3 返回xml格式</a></li><li class="sidebar-sub-header"><a href="/go/web/iris-go/handle-http.html#_3-4-返回html格式" class="sidebar-link">3.4 返回html格式</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/web/iris-go/handle-http.html#_4-简单总结" class="sidebar-link">4 简单总结</a></li></ul></li><li><a href="/go/web/iris-go/route.html" class="sidebar-link">路由操作</a></li><li><a href="/go/web/iris-go/error-handling.html" class="sidebar-link">错误处理</a></li><li><a href="/go/web/iris-go/mvc.html" class="sidebar-link">MVC包的使用</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Go工具中间件</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="处理http请求"><a href="#处理http请求" class="header-anchor">#</a> 处理HTTP请求</h1> <p><a data-fancybox="" title="http-handle" href="/img/goImage/http-handle.png"><img src="/img/goImage/http-handle.png" alt="http-handle"></a></p> <h2 id="_1-数据请求方式的分类"><a href="#_1-数据请求方式的分类" class="header-anchor">#</a> 1 数据请求方式的分类</h2> <blockquote><p>所有的项目中使用的请求都遵循<code>HTTP</code>协议标准,<code>HTTP</code>协议一共定义了八种方法用来对<code>Request-URI</code>网络资源的不同操作方式。HTTP协议经过了1.0和1.1两个版本的发展。</p> <ul><li><code>HTTP1.0</code>定义了三种请求方法：<code>GET</code>, <code>POST</code> 和 <code>HEAD</code>方法。</li> <li><code>HTTP1.1</code>新增了五种请求方法：<code>OPTIONS</code>, <code>PUT</code>, <code>DELETE</code>, <code>TRACE</code> 和 <code>CONNECT</code> 方法。</li></ul></blockquote> <h2 id="_2-iris请求处理方式"><a href="#_2-iris请求处理方式" class="header-anchor">#</a> 2 IRIS请求处理方式</h2> <div class="custom-block tip"><p class="custom-block-title">处理方式概述</p> <ul><li>1、<code>Iris</code>框架中服务实例<code>app</code>中包含多个方法，用来支持对上述<code>HTTP</code>多种请求类型的直接处理，直接定义为<code>get</code>方法、<code>post</code>方法、<code>put</code>方法等，<code>app</code>中包含的自动处理路由请求的方法与<code>http</code>请求类型的分类一致。</li> <li>2、除了上述1中自动处理各类别的请求外，框架还支持使用通用的<code>Handle</code>方法来自定义编写自己的请求处理类型及对应的方法。</li></ul></div> <h3 id="_2-1-get请求"><a href="#_2-1-get请求" class="header-anchor">#</a> 2.1 GET请求</h3> <blockquote><p>向特定的网络资源数据发起请求。<code>GET</code>请求可以携带请求数据，携带的请求数据会以<code>？</code>分割<code>URL</code>和传输数据，参数之间以<code>&amp;</code>相连，比如<a href="http://localhost:8080/getUserInfo?username=admin&amp;password=12345" target="_blank" rel="noopener noreferrer">http://localhost:8080/getUserInfo?username=admin&amp;password=12345<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>。 如下是一个<code>http</code>的<code>get</code>类型的请求示例：</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;fmt&quot;</span>
	<span class="token string">&quot;github.com/kataras/iris/v12&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">//创建一个app结构体指针对象</span>
	app <span class="token operator">:=</span> iris<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token comment">//url: http://localhost:8080/getUserInfo?username=admin&amp;password=12345</span>
	<span class="token comment">//type：GET请求</span>
	<span class="token comment">//param：传递两个参数username、password</span>
	app<span class="token punctuation">.</span><span class="token function">Get</span><span class="token punctuation">(</span><span class="token string">&quot;/getUserInfo&quot;</span><span class="token punctuation">,</span> <span class="token keyword">func</span><span class="token punctuation">(</span>context iris<span class="token punctuation">.</span>Context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token comment">//获取请求的路径</span>
		path <span class="token operator">:=</span> context<span class="token punctuation">.</span><span class="token function">Path</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
		<span class="token comment">//获取get请求传递过来的对应参数值</span>
		username <span class="token operator">:=</span> context<span class="token punctuation">.</span><span class="token function">URLParam</span><span class="token punctuation">(</span><span class="token string">&quot;username&quot;</span><span class="token punctuation">)</span>
		password <span class="token operator">:=</span> context<span class="token punctuation">.</span><span class="token function">URLParam</span><span class="token punctuation">(</span><span class="token string">&quot;password&quot;</span><span class="token punctuation">)</span>
		<span class="token comment">//在控制台打印获取的参数值</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>username<span class="token punctuation">,</span> password<span class="token punctuation">)</span>
		<span class="token comment">//在控制台打印路径</span>
		app<span class="token punctuation">.</span><span class="token function">Logger</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Info</span><span class="token punctuation">(</span>path<span class="token punctuation">)</span>
		<span class="token comment">//在浏览器上打印</span>
		context<span class="token punctuation">.</span><span class="token function">HTML</span><span class="token punctuation">(</span><span class="token string">&quot;&lt;h1&gt;Hello World！！&lt;/h1&gt;&quot;</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span><span class="token punctuation">)</span>
	<span class="token comment">//8080 监听端口 ，如果未发现服务则返回系统自定义的错误</span>
	app<span class="token punctuation">.</span><span class="token function">Run</span><span class="token punctuation">(</span>iris<span class="token punctuation">.</span><span class="token function">Addr</span><span class="token punctuation">(</span><span class="token string">&quot;:8080&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span> iris<span class="token punctuation">.</span><span class="token function">WithoutServerError</span><span class="token punctuation">(</span>iris<span class="token punctuation">.</span>ErrServerClosed<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span>输出结果<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>
admin <span class="token number">12345</span>
<span class="token punctuation">[</span>INFO<span class="token punctuation">]</span> <span class="token number">2020</span><span class="token operator">/</span><span class="token number">01</span><span class="token operator">/</span><span class="token number">07</span> <span class="token number">23</span><span class="token punctuation">:</span><span class="token number">00</span> <span class="token operator">/</span>getUserInfo
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br></div></div><h3 id="_2-2-post请求"><a href="#_2-2-post请求" class="header-anchor">#</a> 2.2 POST请求</h3> <blockquote><p><code>POST</code>请求在进行请求时会将请求数据放在请求<code>body</code>中进行请求，请求数据大小没有限制。在开发过程中，我们使用<code>postman</code>工具来进行<code>POST</code>请求的调试。 <code>POST</code>请求的示例如下所示：</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;fmt&quot;</span>
	<span class="token string">&quot;github.com/kataras/iris/v12&quot;</span>
<span class="token punctuation">)</span>

<span class="token comment">//定义接收json、xml数据的结构体</span>
<span class="token keyword">type</span> User <span class="token keyword">struct</span> <span class="token punctuation">{</span>
	Username <span class="token builtin">string</span> <span class="token string">`json:&quot;username&quot;`</span>
	Password <span class="token builtin">string</span> <span class="token string">`json:&quot;password&quot;`</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">//创建一个app结构体指针对象</span>
	app <span class="token operator">:=</span> iris<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token comment">//url: http://localhost:8080/postLogin</span>
	<span class="token comment">//type：POST请求</span>
	<span class="token comment">//param：form表单传递两个参数username、password</span>
	app<span class="token punctuation">.</span><span class="token function">Post</span><span class="token punctuation">(</span><span class="token string">&quot;/postLogin&quot;</span><span class="token punctuation">,</span> <span class="token keyword">func</span><span class="token punctuation">(</span>context iris<span class="token punctuation">.</span>Context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token comment">//获取请求的路径</span>
		path <span class="token operator">:=</span> context<span class="token punctuation">.</span><span class="token function">Path</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
		<span class="token comment">//获取post请求传递过来的对应参数值</span>
		username <span class="token operator">:=</span> context<span class="token punctuation">.</span><span class="token function">PostValue</span><span class="token punctuation">(</span><span class="token string">&quot;username&quot;</span><span class="token punctuation">)</span>
		password <span class="token operator">:=</span> context<span class="token punctuation">.</span><span class="token function">PostValue</span><span class="token punctuation">(</span><span class="token string">&quot;password&quot;</span><span class="token punctuation">)</span>
		<span class="token comment">//在控制台打印获取的参数值</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>username<span class="token punctuation">,</span> password<span class="token punctuation">)</span>
		<span class="token comment">//在控制台打印路径</span>
		app<span class="token punctuation">.</span><span class="token function">Logger</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Info</span><span class="token punctuation">(</span>path<span class="token punctuation">)</span>

		context<span class="token punctuation">.</span><span class="token function">WriteString</span><span class="token punctuation">(</span><span class="token string">&quot;hello &quot;</span><span class="token operator">+</span>username<span class="token punctuation">)</span>
	<span class="token punctuation">}</span><span class="token punctuation">)</span>

	<span class="token comment">//url: http://localhost:8080/postJson</span>
	<span class="token comment">//type：POST请求</span>
	<span class="token comment">//param：json格式的请求数据</span>
	app<span class="token punctuation">.</span><span class="token function">Post</span><span class="token punctuation">(</span><span class="token string">&quot;/postJson&quot;</span><span class="token punctuation">,</span> <span class="token keyword">func</span><span class="token punctuation">(</span>context iris<span class="token punctuation">.</span>Context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token comment">//声明一个结构体用于接收json数据</span>
		<span class="token keyword">var</span> u1 User
		<span class="token comment">//获取前端传递过来的json数据,放入到结构体中</span>
		err <span class="token operator">:=</span> context<span class="token punctuation">.</span><span class="token function">ReadJSON</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>u1<span class="token punctuation">)</span>
		<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
			fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;获取用户信息错误,err:%v\n&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token punctuation">}</span>
		<span class="token comment">//输出接收到的数据是:main.User{Username:&quot;admin&quot;,Password:123456}</span>
		context<span class="token punctuation">.</span><span class="token function">Writef</span><span class="token punctuation">(</span><span class="token string">&quot;接收到的数据是:%#+v\n&quot;</span><span class="token punctuation">,</span> u1<span class="token punctuation">)</span>
		<span class="token comment">//也可以返回json格式的数据</span>
		<span class="token comment">//context.JSON(iris.Map{&quot;message&quot;: &quot;hello word&quot;, &quot;requestCode&quot;: 200})</span>

	<span class="token punctuation">}</span><span class="token punctuation">)</span>

	<span class="token comment">//url: http://localhost:8080/postXml</span>
	<span class="token comment">//type：POST请求</span>
	<span class="token comment">//param：xml格式的请求数据</span>
	app<span class="token punctuation">.</span><span class="token function">Post</span><span class="token punctuation">(</span><span class="token string">&quot;/postXml&quot;</span><span class="token punctuation">,</span> <span class="token keyword">func</span><span class="token punctuation">(</span>context iris<span class="token punctuation">.</span>Context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token comment">//声明一个结构体用于接收xml数据</span>
		<span class="token keyword">var</span> u1 User
		<span class="token comment">//获取前端传递过来的xml数据,放入到结构体中</span>
		err <span class="token operator">:=</span> context<span class="token punctuation">.</span><span class="token function">ReadXML</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>u1<span class="token punctuation">)</span>
		<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
			fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;获取用户信息错误,err:%v\n&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token punctuation">}</span>
		<span class="token comment">//输出接收到的数据是:main.User{Username:&quot;admin&quot;,Password:123456}</span>
		context<span class="token punctuation">.</span><span class="token function">Writef</span><span class="token punctuation">(</span><span class="token string">&quot;接收到的数据是:%#+v\n&quot;</span><span class="token punctuation">,</span> u1<span class="token punctuation">)</span>
		<span class="token comment">//也可以返回XML格式的数据</span>
		<span class="token comment">//context.XML(User{Username: &quot;admin&quot;, Password: &quot;123456&quot;})</span>
	<span class="token punctuation">}</span><span class="token punctuation">)</span>
	<span class="token comment">//8080 监听端口 ，如果未发现服务则返回系统自定义的错误</span>
	app<span class="token punctuation">.</span><span class="token function">Run</span><span class="token punctuation">(</span>iris<span class="token punctuation">.</span><span class="token function">Addr</span><span class="token punctuation">(</span><span class="token string">&quot;:8080&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span> iris<span class="token punctuation">.</span><span class="token function">WithoutServerError</span><span class="token punctuation">(</span>iris<span class="token punctuation">.</span>ErrServerClosed<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br></div></div><h3 id="_2-3-put-delete等请求"><a href="#_2-3-put-delete等请求" class="header-anchor">#</a> 2.3 PUT &amp; DELETE等请求</h3> <blockquote><p>除了上述<code>GET</code>、<code>POST</code>最为常见的两种请求方式以外，还有<code>PUT</code>、<code>DELETE</code>、<code>OPTIONS</code>、<code>HEAD</code>等其他类型请求，对于其他类型的请求，如同<code>GET</code>和<code>POST</code>请求一样进行处理。示例如下</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;fmt&quot;</span>
	<span class="token string">&quot;github.com/kataras/iris/v12&quot;</span>
<span class="token punctuation">)</span>

<span class="token comment">//定义接收json、xml数据的结构体</span>
<span class="token keyword">type</span> User <span class="token keyword">struct</span> <span class="token punctuation">{</span>
	Username <span class="token builtin">string</span> <span class="token string">`json:&quot;username&quot;`</span>
	Password <span class="token builtin">string</span> <span class="token string">`json:&quot;password&quot;`</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">//创建一个app结构体指针对象</span>
	app <span class="token operator">:=</span> iris<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token comment">//type：PUT类型请求</span>
	app<span class="token punctuation">.</span><span class="token function">Put</span><span class="token punctuation">(</span><span class="token string">&quot;/putInfo&quot;</span><span class="token punctuation">,</span> <span class="token keyword">func</span><span class="token punctuation">(</span>context iris<span class="token punctuation">.</span>Context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		path <span class="token operator">:=</span> context<span class="token punctuation">.</span><span class="token function">Path</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
		app<span class="token punctuation">.</span><span class="token function">Logger</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Info</span><span class="token punctuation">(</span><span class="token string">&quot;请求url：&quot;</span><span class="token punctuation">,</span> path<span class="token punctuation">)</span>
		<span class="token keyword">var</span> u1 User
		<span class="token comment">//获取前端传递过来的json数据,放入到结构体中</span>
		err <span class="token operator">:=</span> context<span class="token punctuation">.</span><span class="token function">ReadJSON</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>u1<span class="token punctuation">)</span>
		<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
			fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;获取用户信息错误,err:%v\n&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token punctuation">}</span>
		<span class="token comment">//输出接收到的数据是:main.User{Username:&quot;admin&quot;,Password:123456}</span>
		context<span class="token punctuation">.</span><span class="token function">Writef</span><span class="token punctuation">(</span><span class="token string">&quot;接收到的数据是:%#+v\n&quot;</span><span class="token punctuation">,</span> u1<span class="token punctuation">)</span>
		<span class="token comment">//也可以返回json格式的数据</span>
		<span class="token comment">//context.JSON(iris.Map{&quot;message&quot;: &quot;hello word&quot;, &quot;requestCode&quot;: 200})</span>
	<span class="token punctuation">}</span><span class="token punctuation">)</span>
	<span class="token comment">//	DELETE请求</span>
	<span class="token comment">//type：DELETE类型请求</span>
	app<span class="token punctuation">.</span><span class="token function">Delete</span><span class="token punctuation">(</span><span class="token string">&quot;/deleteUser&quot;</span><span class="token punctuation">,</span> <span class="token keyword">func</span><span class="token punctuation">(</span>context iris<span class="token punctuation">.</span>Context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		path <span class="token operator">:=</span> context<span class="token punctuation">.</span><span class="token function">Path</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
		app<span class="token punctuation">.</span><span class="token function">Logger</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Info</span><span class="token punctuation">(</span><span class="token string">&quot;Delete请求url：&quot;</span><span class="token punctuation">,</span> path<span class="token punctuation">)</span>
	<span class="token punctuation">}</span><span class="token punctuation">)</span>
	<span class="token comment">//8080 监听端口 ，如果未发现服务则返回系统自定义的错误</span>
	app<span class="token punctuation">.</span><span class="token function">Run</span><span class="token punctuation">(</span>iris<span class="token punctuation">.</span><span class="token function">Addr</span><span class="token punctuation">(</span><span class="token string">&quot;:8080&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span> iris<span class="token punctuation">.</span><span class="token function">WithoutServerError</span><span class="token punctuation">(</span>iris<span class="token punctuation">.</span>ErrServerClosed<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br></div></div><h2 id="_3-请求处理的数据格式返回"><a href="#_3-请求处理的数据格式返回" class="header-anchor">#</a> 3 请求处理的数据格式返回</h2> <blockquote><p>我们已经学习了如何对不同类型的请求进行处理以及如何获取请求所携带的数据，当后台接收到请求后，将会对请求进行处理，处理完毕后将数据返回给请求的客户端。接下来，我们看一看如何将数据进行返回，以及都有哪些形式。
在进行请求处理时，处理方法<code>func</code>有一个参数<code>context</code>。<code>Context</code>是用于处理请求的上下文环境变量，用于处理<code>http</code>请求及相关数据返回。<code>iris</code>框架支持多种数据格式的返回，此处我们学习掌握返回<code>string、json、xml</code>以及<code>html</code>格式的数据。</p></blockquote> <h3 id="_3-1-返回string类型"><a href="#_3-1-返回string类型" class="header-anchor">#</a> 3.1 返回string类型</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code>context<span class="token punctuation">.</span><span class="token function">WriteString</span><span class="token punctuation">(</span><span class="token string">&quot;hello world&quot;</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_3-2-返回json格式"><a href="#_3-2-返回json格式" class="header-anchor">#</a> 3.2 返回json格式</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code>context<span class="token punctuation">.</span><span class="token function">JSON</span><span class="token punctuation">(</span>iris<span class="token punctuation">.</span>Map<span class="token punctuation">{</span><span class="token string">&quot;message&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;hello word&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;requestCode&quot;</span><span class="token punctuation">:</span> <span class="token number">200</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_3-3-返回xml格式"><a href="#_3-3-返回xml格式" class="header-anchor">#</a> 3.3 返回xml格式</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code>context<span class="token punctuation">.</span><span class="token function">XML</span><span class="token punctuation">(</span>User<span class="token punctuation">{</span>Username<span class="token punctuation">:</span> <span class="token string">&quot;admin&quot;</span><span class="token punctuation">,</span> Password<span class="token punctuation">:</span> <span class="token number">123456</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_3-4-返回html格式"><a href="#_3-4-返回html格式" class="header-anchor">#</a> 3.4 返回html格式</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code>context<span class="token punctuation">.</span><span class="token function">HTML</span><span class="token punctuation">(</span><span class="token string">&quot;&lt;h1&gt; Hello World &lt;/h1&gt;&quot;</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="_4-简单总结"><a href="#_4-简单总结" class="header-anchor">#</a> 4 简单总结</h2> <table><tr><th style="width:25%;text-align:left;"> 数据操作</th><th style="width:45%;text-align:left;">示例</th><th style="width:30%;text-align:center;">描述</th></tr> <tr><td>获取json数据参数</td><td>context.ReadJSON(&amp;user)</td><td>用context获取Json数据到结构体user里面</td></tr> <tr><td>获取xml数据参数	</td><td>context.ReadXML(&amp;user)</td><td>用context获取xml数据到结构体user里面</td></tr> <tr><td>获取form表单参数</td><td>context.PostValue(&quot;username&quot;)</td><td>用context获取form表单中对应key的数据</td></tr> <tr><td>获取url地址参数	</td><td>context.URLParam(&quot;username&quot;)</td><td>用context获取url地址中的参数数据 </td></tr> <tr><td>返回string类型	</td><td>contable text.WriteString(&quot;hello world&quot;)</td><td>用context返回字符串给调用者</td></tr> <tr><td>返回xml格式     </td><td>context.XML(User{Username: &quot;admin&quot;, Password: 123456})</td><td>用context返回xml格式数据给调用者</td></tr> <tr><td>返回html格式    </td><td>context.HTML(&quot;Hello World&quot;)</td><td> 用context返回html给调用者</td></tr> <tr><td>返回json格式    </td><td>context.JSON(iris.Map{&quot;message&quot;: &quot;hello word&quot;, &quot;requestCode&quot;: 200})</td><td>用context返回json数据给调用者</td></tr></table></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/go/web/iris-go/config.html" class="prev">
        配置信息
      </a></span> <span class="next"><a href="/go/web/iris-go/route.html">
        路由操作
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.1bc80adb.js" defer></script><script src="/assets/js/2.7c0608ab.js" defer></script><script src="/assets/js/100.05bb269d.js" defer></script>
  </body>
</html>
